package cn.iocoder.yudao.module.dbit.controller.admin.resumeinfo;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dbit.controller.admin.resumeinfo.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.resumeinfo.ResumeInfoDO;
import cn.iocoder.yudao.module.dbit.service.resumeinfo.ResumeInfoService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 简历信息")
@RestController
@RequestMapping("/dbit/resume-info")
@Validated
public class ResumeInfoController {

    @Resource
    private ResumeInfoService resumeInfoService;
    @Resource
    private FileService fileService;

    @PostMapping("/create")
    @Operation(summary = "上传简历信息")
    public CommonResult<Integer> createResumeInfo(ResumeUploadReqVO resumeUploadReqVO) throws IOException {
        MultipartFile file = resumeUploadReqVO.getFile();
        String path = resumeUploadReqVO.getPath();
        String resumeName = file.getOriginalFilename();
        String resumePath = fileService.createFile(resumeName, path, IoUtil.readBytes(file.getInputStream()));
        ResumeInfoSaveReqVO createReqVO = new ResumeInfoSaveReqVO();
        createReqVO.setResumeName(resumeName);
        createReqVO.setResumePath(resumePath);
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        createReqVO.setUserId(loginUserId != null ? loginUserId.intValue() : 0);
        return success(resumeInfoService.createResumeInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新简历信息")
    public CommonResult<Boolean> updateResumeInfo(@Valid @RequestBody ResumeInfoSaveReqVO updateReqVO) {
        resumeInfoService.updateResumeInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除简历信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteResumeInfo(@RequestParam("id") Integer id) {
        resumeInfoService.deleteResumeInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得简历信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ResumeInfoRespVO> getResumeInfo(@RequestParam("id") Integer id) {
        ResumeInfoDO resumeInfo = resumeInfoService.getResumeInfo(id);
        return success(BeanUtils.toBean(resumeInfo, ResumeInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得简历信息分页")
    public CommonResult<PageResult<ResumeInfoRespVO>> getResumeInfoPage(@Valid ResumeInfoPageReqVO pageReqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        pageReqVO.setUserId(loginUserId != null ? loginUserId.intValue() : 0);
        PageResult<ResumeInfoDO> pageResult = resumeInfoService.getResumeInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResumeInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出简历信息 Excel")
    @PreAuthorize("@ss.hasPermission('dbit:resume-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportResumeInfoExcel(@Valid ResumeInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResumeInfoDO> list = resumeInfoService.getResumeInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "简历信息.xls", "数据", ResumeInfoRespVO.class,
                        BeanUtils.toBean(list, ResumeInfoRespVO.class));
    }

}